define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/importquestiontask/index' + location.search,
                    add_url: 'user/importquestiontask/add',
                    //edit_url: 'user/importquestiontask/edit',
                    //del_url: 'user/importquestiontask/del',
                    multi_url: 'user/importquestiontask/multi',
                    import_url: 'user/importquestiontask/import',
                    table: 'import_question_task',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='course.id']", form).addClass("selectpage").data("source", "user/course/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: __('编号'),
                            searchable:false
                        },
                        {
                            field: 'course.name',
                            title: __('课程'),
                            searchable: false
                        },
                        {field: 'paper.name', title: __('试卷'), operate: 'LIKE'},
                        {
                            field: 'file_path',
                            title: __('文件'),
                            formatter: Table.api.formatter.file,
                            searchable:false
                        },
                        {
                            field: 'status',
                            title: __('进度'),
                            formatter: Table.api.formatter.normal,
                            searchList: {'1': __('等待导入'), '2': __('已导入'), '3': __('导入失败')}
                        },
                        {
                            field: 'error',
                            title: __('回复'),
                            searchable:false
                        },
                        {field: 'course.id', title: __('课程'), visible: false},
                        {
                            field: 'update_time',
                            title: __('时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        }
                    ]
                ]
            });

            // 监听导入题库事件
            $('.btn-add-question').click(function () {
                var selectIds = Table.api.selectedids(table);
                var url = 'user/paper/importquestion/ids/' + selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目-Excel'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导出题库事件
            $('.btn-export-question').click(function () {
                var selectIds = Table.api.selectedids(table);
                var url = 'user/paper/exportquestion/ids/' + selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导出题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听清空题目事件
            $('.btn-clear-question').click(function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定清空选中试卷的题目数据?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true, btn: [__('OK'), __('Cancel')]},
                    function (index) {
                        Table.api.multi("clearquestion", ids, table, that);
                        Layer.close(index);
                    }
                );
            });

            // 监听AI导入题库事件
            $('.btn-ai-add-question').click(function () {
                var selectIds = Table.api.selectedids(table);
                var url = 'user/paper/aiimportquestion/ids/' + selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目-WORD'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听查看导入进度事件
            $('.btn-import-question-task').click(function () {
                var url = 'user/importquestiontask/index';
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('人工导题进度查询'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-category_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        importquestion: function () {
            Controller.api.bindevent();
        },
        aiimportquestion: function () {
            Controller.api.bindevent();
        },
        exportquestion: function () {
            Controller.api.bindevent();
            Form.api.bindevent("form[role=form]", function (data, ret) {
                window.location.href = data;
                return true;
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
